package personalProfile;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ResourceBundle;

import managers.DBManager;
import managers.socialNetworkManagers.SocialNetworkFeedbackManager;
import managers.socialNetworkManagers.SocialNetworkManager;
import spotGUI.personalProfileGUI.PersonalDataPanel;
import spotGUI.personalProfileGUI.ProfileUpdateLogger;
import spotGUI.personalProfileGUI.Updatable;
import utils.StackTracePrinter;
import elements.PersonalData;

public class PersonalDataManager implements Updatable {

	private PersonalDataPanel view;
	private int userId;
	private ProfileUpdateLogger logger;

	public PersonalDataManager (int id, ProfileUpdateLogger logger) {
		view = new PersonalDataPanel(this);
		userId = id;
		this.logger = logger;
		new Thread () {
			public void run() {
				try {
					view.fillFields(retrievePersonalData(DBManager.getConnection()));
				} catch (SQLException e) {
					SocialNetworkManager.getSharedInstance().getFeedbackManager().updateStatus(ResourceBundle.getBundle("personalProfile.messages").getString("m.2"),
							SocialNetworkFeedbackManager.WARNING);
					StackTracePrinter.print(e);
				}
			}
		}.start();
	}


	public PersonalData retrievePersonalData (Connection conn) throws SQLException {
		Statement stmt = conn.createStatement();
		String query = "select * from spotmessenger.USERS U left join spotmessenger.PERSONAL_DATA P on U.id=P.id where U.id="+userId;
		ResultSet rs = stmt.executeQuery(query);

		if (rs.next()) {

			PersonalData pd = new PersonalData(rs.getString("aboutme"), rs.getTimestamp("birthdate"), rs.getString("birthplace"),
					rs.getString("school"), rs.getString("class"), rs.getString("section"), rs.getString("email"),
					rs.getString("city"), rs.getString("route"), rs.getString("homephone"), rs.getString("mobilephone"),
					rs.getString("mysport"), rs.getString("myteam"),
					rs.getString("bestsport"), rs.getString("bestteam"), rs.getString("bestsportsman"),
					rs.getString("bestmusicalgenre"), rs.getString("bestsinger"), rs.getString("bestbook"),
					rs.getString("bestfilm"), rs.getString("bestactor"), rs.getString("bestsong"));

			DBManager.closeResources(stmt, rs);
			return pd;
		} else {
			return new PersonalData();
		}
	}


	@Override
	public void edit() {
		view.setEditable(true);
	}


	@Override
	public void update() {
		try {
			doUpdate(DBManager.getConnection());
		} catch (SQLException e) {
			logger.addError(ResourceBundle.getBundle("personalProfile.messages").getString("l.2"));
			StackTracePrinter.print(e);
		}
		view.setEditable(false);
	}


	private void doUpdate(Connection conn) throws SQLException {
		PersonalData data = view.createNewPersonalData();

		conn.setAutoCommit(false);

		Statement stmt = conn.createStatement();
		String query = "update spotmessenger.personal_data set ";
		query+= "aboutme = '"+data.aboutMe+"' , ";
		query+= "bestactor = '"+data.bestActor+"' , ";
		query+= "bestbook = '"+data.bestBook+"' , ";
		query+= "bestfilm = '"+data.bestFilm+"' , ";
		query+= "bestmusicalgenre = '"+data.bestMusicalGenre+"' , ";
		query+= "bestsinger = '"+data.bestSinger+"' , ";
		query+= "bestsong = '"+data.bestSong+"' , ";
		query+= "bestsport = '"+data.bestSport+"' , ";
		query+= "bestsportsman = '"+data.bestSportsman+"' , ";
		query+= "bestteam = '"+data.bestTeam+"' , ";
		if (data.birthdate != null)
			query+= "birthdate = '"+data.birthdate+"' , ";
		query+= "birthplace = '"+data.birthplace+"' , ";
		query+= "city = '"+data.city+"' , ";
		query+= "homephone = '"+data.homePhone+"' , ";
		query+= "mobilephone = '"+data.mobilePhone+"' , ";
		query+= "mysport = '"+data.mySport+"' , ";
		query+= "myteam = '"+data.myTeam+"' , ";
		query+= "route = '"+data.route+"' , ";
		query+= "school = '"+data.school+"' , ";
		query+= "class = '"+data.schoolClass+"' , ";
		query+= "section = '"+data.section+"' ";
		query+="where id = "+userId;
		stmt.addBatch(query);

		String query2 = "update spotmessenger.users set email = '"+data.email+"' where id = "+userId;
		stmt.addBatch(query2);

		stmt.executeBatch();

		conn.commit();
		conn.setAutoCommit(true);
		DBManager.closeResources(stmt, null);

	}


	public PersonalDataPanel getView() {
		return view;
	}


}
